package com.hd.trans.network;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.hd.trans.network.bean.UserInfoBean;
import com.hd.trans.ui.base.AppContextProvider;
import com.hd.trans.ui.base.TransInit;
import com.hd.trans.utils.DateUtil;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;
import java.util.Date;

/**
 * Author:  yang
 * Date:    2020/01/19 16:48
 * Desc:    Preference管理类
 */
public class PreferenceMgr {
    private CommonPreference commonPreference;
    //    private ConfigPreference configPreference;
    private TranslatePreference translatePreference;

    private static PreferenceMgr mInstance;


    /**
     * 获取TestMgr
     *
     * @return
     */
    public static PreferenceMgr getInstance() {
        if (mInstance == null) {
            init(AppContextProvider.getContext());
        }
        return mInstance;
    }

    public static synchronized void init(@NonNull Context context) {
        if (mInstance == null) {
            mInstance = new PreferenceMgr(context);
        }
    }

    public PreferenceMgr(@NonNull Context context) {
        if (commonPreference == null) {
            PreferenceProxy proxy = new PreferenceProxy();
            commonPreference = proxy.create(context.getApplicationContext(), CommonPreference.class);
            translatePreference = proxy.create(context.getApplicationContext(), TranslatePreference.class);
            /*configPreference = proxy.create(context.getApplicationContext(), ConfigPreference.class);
            userPreference = proxy.create(context.getApplicationContext(), UserPreference.class);*/
        }
    }


    public CommonPreference getCommonPreference() {
        return commonPreference;
    }

   /* public ConfigPreference getConfigPreference() {
        return configPreference;
    }*/

    public TranslatePreference getTranslatePreference() {
        return translatePreference;
    }

    /*public UserPreference getUserPreference() {
        return userPreference;
    }*/
    public boolean isOpenOfflineTrans(){
        return commonPreference.isOpenOfflineTrans();
    }

    public void setOpenOfflineTrans(boolean isOpenOfflineTrans){
        commonPreference.setOpenOfflineTrans(isOpenOfflineTrans);
    }

    /**
     * 保存服务器时间戳
     */
    public void saveServerTimeStamp(long serverTimeStamp) {
        commonPreference.saveTimeDistance(serverTimeStamp - System.currentTimeMillis());
    }

    public long getServerTimeStamp() {
        return getServerDiffTime() + System.currentTimeMillis();
    }

    public long getServerDiffTime() {
        return commonPreference.getTimeDistance();
    }

    /**
     * 获取当天日期(以服务器时间为准)
     */
    public String getCurDate() {
        String curDate = DateUtil.getDateStr3(getServerTimeStamp());
        return curDate;
    }


    public int getDateDifference(Long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(getServerTimeStamp()));
        int year = calendar.get(Calendar.YEAR);

        Calendar targetCalendar = Calendar.getInstance();
        targetCalendar.setTime(new Date(time));
        int targetyear = targetCalendar.get(Calendar.YEAR);

        return targetyear - year;
    }

    public void setShowNewOVal(boolean isShow){
        commonPreference.setShowNewOval(isShow);
    }
    public boolean isShowNewOval(){
        return commonPreference.isShowNewOval();
    }

    public boolean isNotificationDialogEnable() {//该通知暂时只展示一次
        return commonPreference.notificationDialogLastTime() == 0L;
    }

    public void saveNotificationDialogTime() {
        commonPreference.saveNotificationDialogTime(System.currentTimeMillis());
    }

    public void saveMainActivityTime(long timeMillis) {
        commonPreference.saveMainActivityTime(timeMillis);
    }

    public long getMainActivityTime() {
        return commonPreference.getMainActivityTime();
    }

    public void saveMainActivityShowTime(long timeMillis) {
        commonPreference.saveMainActivityShowTime(timeMillis);
    }

    public long getMainActivityShowTime() {
        return commonPreference.getMainActivityShowTime();
    }

    public void saveMainActivityDate(int date) {
        commonPreference.saveMainActivityDate(date);
    }

    public long getMainActivityDate() {
        return commonPreference.getMainActivityDate();
    }

    public void saveWordTransDemoStatus(boolean show) {
        commonPreference.saveWordTransDemoStatus(show);
    }

    public boolean getWordTransDemoStatus() {
        return commonPreference.getWordTransDemoStatus();
    }

    public void saveTransTimes() {
        if(commonPreference.getTransTimes() > 0){
            int value = commonPreference.getTransTimes() -1;
            commonPreference.saveTransTimes(value);
        }
    }

    public void setTransTimes(int value) {
        if(commonPreference.getTransTimes() < 0){
            commonPreference.saveTransTimes(value);
        }
    }

    public long getTransTimes() {
        if(commonPreference.getTransTimes() < 0){
            commonPreference.saveTransTimes(3);
        }
        return commonPreference.getTransTimes();
    }

    public void saveDaoVersion(int version){
        commonPreference.saveDaoVersion(version);
    }

    public int getDaoVersion(){
        return commonPreference.getDaoVersion();
    }

    public void setLanguageVersion(int version){
        commonPreference.setLanguageVersion(version);
    }

    public int getLanguageVersion(){
        return commonPreference.getLanguageVersion();
    }

    public void setAppVersion(int version){
        commonPreference.setAppVersion(version);
    }

    public int getAppVersion(){
        return commonPreference.getAppVersion();
    }

    public void setNeverShowThisTips(boolean isNeverShowThisTips){
        commonPreference.setNeverShowThisTips(isNeverShowThisTips);
    }
    public boolean isNeverShowThisTips() {
        return commonPreference.isNeverShowThisTips();
    }

    public boolean isExperienced() {
        return commonPreference.isExperienced();
    }

    public void setExperienced(boolean experienced) {
        commonPreference.setExperienced(experienced);
    }

    public boolean isExperiencedClose() {
        return commonPreference.isExperiencedClose();
    }

    public void setExperiencedClose(boolean experienced) {
        commonPreference.setExperiencedClose(experienced);
    }

    public void saveExampleCreateTime(long createTime) {
        commonPreference.saveExampleCreateTime(createTime);
    }

    public long getExampleCreateTime() {
        return commonPreference.getExampleCreateTime();
    }

    public boolean isTongNewTip() {
        return commonPreference.isTongNewTip();
    }

    public void setTongNewTip(boolean already) {
        commonPreference.setTongNewTip(already);
    }

    public boolean isAllowSaveChecked() {
        return commonPreference.isAllowSaveChecked();
    }

    public void setAllowSaveChecked(boolean isChecked) {
        commonPreference.setAllowSaveChecked(isChecked);
    }

    public UserInfoBean getUserInfo() {
        if (TextUtils.isEmpty(commonPreference.getUserInfo())) {
            return null;
        }
        return new Gson().fromJson(commonPreference.getUserInfo(), UserInfoBean.class);
    }

    public void setUserInfo(UserInfoBean userInfoBean) {
        if(userInfoBean != null){
            commonPreference.setUserInfo(new Gson().toJson(userInfoBean));
        }else {
            commonPreference.setUserInfo(null);
        }
    }

    public void setAiTransLimitDate(long time) {
        commonPreference.setAiTransLimitDate(time);
    }

    public long getAiTransLimitDate() {
        return commonPreference.getAiTransLimitDate();
    }

    public void setAiTransRemainderLimitNumber(int time) {
        commonPreference.setAiTransRemainderLimitNumber(time);
    }

    public int getAiTransRemainderLimitNumber() {
        return commonPreference.getAiTransRemainderLimitNumber();
    }

    public void setAiTransVipLimitNumber(int time) {
        commonPreference.setAiTransVipLimitNumber(time);
    }

    public int getAiTransVipLimitNumber() {
        return commonPreference.getAiTransVipLimitNumber();
    }
    public void setAiTransLimitNumber(int time) {
        commonPreference.setAiTransLimitNumber(time);
    }

    public int getAiTransLimitNumber() {
        return commonPreference.getAiTransLimitNumber();
    }

    public void setAiPrefix(String prefix) {
        commonPreference.setAiPrefix(prefix);
    }

    public String getAiPrefix() {
        return commonPreference.getAiPrefix();
    }

    public void setAiSuffix(String suffix) {
        commonPreference.setAiSuffix(suffix);
    }

    public String getAiSuffix() {
        return commonPreference.getAiSuffix();
    }
}
